<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    slash<phpunion@gmail.com>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: info_supplier_person.class.php 2012-04-24 19:22:04 $
 * @Show Note: 供应商负责人管理
 */
Class info_supplier_person extends TheAdmin
{
		/* DataBase object */
		public $db;
	
		/* View the data page limit items */
		public $pageSize = 10;

		/** 供应商 **/
		public $supplier_arr = array();
		
		/**
		 *	Construct this module class.
		*/
		public function info_supplier_person()
		{
		}
		public function set()
		{
			$this->TheAdmin();
			//定义模版
			$this->tplFile ="info_supplier_person.tpl";
			$this->admin_user_id = $this->userRequest->get('admin_user_id');
			//取得数据库连接
			$this->db = Factory::getDB("info_supplier_person");
			//取得用户对于该类的权限
			$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
			$class_p_arr = $this->show_p();
			//取得该类的权值数组
			$class_method = $class_p_arr['class_method'];
			//取得用户操作菜单
			$menu = array();
			foreach ($class_method as $k=>$v)
			{
				if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
				{
					array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
				}
			}

			/** 取得用户信息 **/
			$this->user_info = $this->get_user_info($this->admin_user_id);
			$this->supplier_arr = $this->getSupplier();
			//获得供应商信息
			$this->smarty->assign('supplier_arr',$this->get_supplier());
			//用户有权菜单生成 可以添加其他操作
			$this->smarty->assign('menus',$menu);
			$this->smarty->assign("main_menus",$this->get_user_menus());
		}
	
		/**
		 *	Call class construct method.
		*/
		public function __construct()
		{
				$this->info_supplier_person();
		}
		public function _display()
		{
			$this->smarty->display($this->tplFile);
		}
		/**
		 *	default method.
		*/
		public function show()
		{
			$supplier_id = isset($_GET['supplier_id']) ? intval($_GET['supplier_id']) : 0;
			if($supplier_id > 0 ) {
				$where = "supplier_id = '{$supplier_id}'";
			} else {
			    $where = 1;
			}
				$col = "person_id";
				if($_GET['supplier_id']){
					$where .= " and supplier_id={$_GET['supplier_id']}";
				}
				$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
				//数据库执行sql语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if( 0 > $rowNo) goToIndex($rowNo);
				//分页
				$page = Factory::getBaseClass('pagination');
				$page->set($rowNo,$this->pageSize);
				$numrows = $this->pageSize;
				$offset  = $page->getSqlMove();
				$arr = $this->db->getAll($where,$col,$numrows,$offset);
				admin_log($this->admin_user_id,__METHOD__,'查看',1);
				//数据库执行select语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if(0 > $arr ) goToIndex($arr);
				
				$this->smarty->assign('arr',$arr);
				$this->smarty->assign('supplier_arr',$this->supplier_arr );
				$this->smarty->assign('supplier_id',$supplier_id);
				
				//默认方法
				$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
				$this->smarty->assign('act','list');
				$this->smarty->display($this->tplFile);
		}
		
		/**
		 *	add method.
		*/
		public function add()
		{
			$supplier_id = isset($_GET['supplier_id']) ? intval($_GET['supplier_id']) : 0;
			if(empty($supplier_id)) {
				alertMsg2("供应商不存在,请重新操作", 'index.php?c=info_supplier');
			}

				//添加数据
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						$this->smarty->assign('supplier_id',$supplier_id);

						//使用模版，展示提交界面
						$this->smarty->assign('act_title','添加');						
						$this->smarty->assign('act','add');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						$temp = $this->getValue();
						$temp['supplier_id'] = $supplier_id;
						$insrt = $this->db->insert_info_supplier_person($temp);
						$error_no = 0 > $insrt ?  $insrt :'-1501';
						admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						alertMsg($error_no, 'index.php?c=info_supplier_person&supplier_id='.$supplier_id);
				}
		}
		
		/**
		 *	edit method.
		*/
		public function edit()
		{
			$supplier_id = isset($_GET['supplier_id']) ? intval($_GET['supplier_id']) : 0;
			if(empty($supplier_id)) {
				alertMsg2("供应商不存在，请重新操作", 'index.php?c=info_supplier');
			}

				//取得id，也就是唯一索引
				$person_id = isset($_REQUEST['person_id']) ? intval(trim($_REQUEST['person_id'])) : 0 ;
				//取得是否查看详情
				$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						//使用模版，展示提交界面
						$result = $this->db->getOne($person_id);
						if(empty($result))
						{
							alertMsg('-2001', 'index.php?c=info_supplier_person');
						}
						foreach ($result AS $k=>$v)
						{
							$this->smarty->assign("{$k}",$v);
						}
						$this->smarty->assign('supplier_id',$supplier_id);

						$this->smarty->assign('is_show',$is_show);
						$this->smarty->assign('act_title','修改');
						$this->smarty->assign('act','edit');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						//设置where条件
						$where = "person_id=$person_id";
						$temp = $this->getValue(1);
						$update = $this->db->update_info_supplier_person($temp,$where);
						admin_log($this->admin_user_id,__METHOD__,$where,2);
						$error_no = 0 > $update ?  $update :'-1502';
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						alertMsg($error_no, 'index.php?c=info_supplier_person&supplier_id='.$supplier_id);
				}
		}
		
		/**
		 *	del method.
		*/
		public function del()
		{
				//取得id，也就是唯一索引
				$person_id = isset($_REQUEST['person_id']) ? intval(trim($_REQUEST['person_id'])) : 0 ;
				$where = "person_id=$person_id";
				$del=$this->db->delete_info_supplier_person($where);
				admin_log($this->admin_user_id,__METHOD__,$where,3);
				$error_no = 0 > $del ?  $del :'-1503';
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				alertMsg($error_no, 'index.php?c=info_supplier_person');
		}
		
		
		public function get_supplier(){
			$db = Factory::getDb('info_supplier_person');
			$arr = $db->getAll(1);
			$new_arr = array();
			if($arr){
				foreach($arr as $v){
					$new_arr[$v['supplier_id']] = $v['supplier_name'];
				}
			}
			return $new_arr;
		}
		/**
		 *	get request method.
		*/
		public function &getValue($isEdit=0)
		{
			$temp = array();
			$check = Factory::getBaseClass("checkBase");
			if($isEdit){
				$temp['person_id'] = $_POST['person_id'];
			} else {
				$temp['person_id'] = "";
			}
			//补充其他的字段
			$temp['person_name'] = $_POST['person_name'];
			$temp['person_mob'] = $_POST['person_mob'];
			$temp['person_tel'] = $_POST['person_tel'];
			$temp['person_mail'] = $_POST['person_mail'];
			$temp['person_qq'] = $_POST['person_qq'];
			$temp['person_msn'] = $_POST['person_msn'];
			$temp['person_birthday'] = $_POST['person_birthday'];
			$temp['person_sex'] = $_POST['person_sex'];
			//$temp['laster'] = $_POST['laster'];
			$temp['laster'] = $this->admin_user_id;
			$temp['update_time'] = time();
			//$temp['supplier_id'] = $_POST['supplier_id'];
			admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
			return $temp;
		}
		/**
		 *	get class method and popedom.
		*/
		public function show_p()
		{
			//该方法为，提供用户使用方法定义，非web用户权限相关不用写
			return array(
				/* 定义该类中文名称*/
				//'class_name'=>array(__CLASS__,'供应商负责人管理'),
				'class_method' =>array(
					//array（方法名，中文解释，权值,是否为菜单）
					'show'  => array('show','列表',1,false),
					'add'  => array('add','添加',2,false),
					'edit'  => array('edit','编辑',4,false),
					'del'  => array('del','删除',8,false),
					'show_p'=> array('show_p','',0,false)
				)
			);
		}

		/** 获取用户供应商 **/
		public function getSupplier()
		{
			$result = array();

			$db_is = Factory::getDB("info_supplier");
			$where = "admin_user_id = '{$this->admin_user_id}'";
			$res = $db_is->getAll($where);
			if(is_array($res)) {
				foreach($res as $k =>$v) {
					$result[$v['supplier_id']] = $v;
				}
			}
			return $result;			
		}
}

?>